package com.oplus.config;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 
 * @author luoweifeng
 * 
 */
public class ConfigBuilder {
	private static final String FSPROPERTY = "database.properties";
	private static Properties paraProps = null;

//	public ConfigBuilder() {
//		loadFile(FSPROPERTY);
//	}
//
//	public ConfigBuilder(String confFile) {
//		loadFile(confFile);
//	}

	private static void loadFile(String confFile) {
		if (confFile != null && confFile.trim().length() > 0) {
			InputStream is = ConfigBuilder.class
					.getResourceAsStream("/" + confFile);
			try {
				paraProps =new Properties();
				paraProps.load(is);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				System.err.println("不能读取配置文件" + FSPROPERTY + "请确保文件路径正确");
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public static String getProperty(String key, String defaultValue) {
		if(paraProps == null){
			loadFile(FSPROPERTY);
		}
		return paraProps.getProperty(key, defaultValue);
	}

	public static String getProperty(String proName) {
		if(paraProps == null){
			loadFile(FSPROPERTY);
		}
		return paraProps.getProperty(proName);
	}
	
	private static void setProperty(String proName,String value) {
		if(paraProps == null){
			loadFile(FSPROPERTY);
		}
		paraProps.setProperty(proName,value);
		
		
	}
	
	public static String getURLKey() {
		return getProperty("oplus.jdbc.url");
	}
	
	public static String getUserKey() {
		return getProperty("oplus.jdbc.username");
	}
	
	public static String getPassword() {
		return getProperty("oplus.jdbc.password");
	}
	
	public static void setURLKey(String value) {
		setProperty("oplus.jdbc.url",value);
	}
	
	public static void setUserKey(String value) {
		setProperty("oplus.jdbc.username",value);
	}
	
	public static void setPassword(String value) {
		setProperty("oplus.jdbc.password",value);
	}

	
	public static void toStoreFile() throws IOException {
		String fileName = "./"+FSPROPERTY;
		String path = ConfigBuilder.class.getClassLoader().getResource("").getPath();
		
		if (paraProps != null) {
//			File dfile = new File(fileName.substring(0, fileName
//					.lastIndexOf("/")));
//			if (!dfile.exists())
//				dfile.mkdirs();

			File file = new File(fileName);
			System.out.println(Thread.currentThread().getContextClassLoader().getResource("")); 

			System.out.println(ConfigBuilder.class.getClassLoader().getResource("")); 
			System.out.println(ClassLoader.getSystemResource("")); 
			System.out.println(ConfigBuilder.class.getResource("")); 
			System.out.println(ConfigBuilder.class.getResource("/")); //Class文件所在路径 
			System.out.println(new File("/").getAbsolutePath()); 
			System.out.println(System.getProperty("user.dir")); 
			
//			InputStream file1 = ConfigBuilder.class.getClassLoader().getResourceAsStream(fileName).;
			FileOutputStream writer = new FileOutputStream(fileName);
			paraProps.store(writer, "FF");
//			writer.close();
		}
	}
}